/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ResultadoProdEnergia.java
 *
 * Created on 01/08/2010, 20:05:22
 */

package br.leiam.eolica.denis;

import java.awt.Component;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.text.NumberFormat;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;

import org.jfree.chart.ChartUtilities;

import sun.misc.BASE64Encoder;

/**
 * 
 * @author Denis
 */
public class ResultadoProdEnergia extends javax.swing.JPanel {

	private AbstractTableModel tableModel;// = new AbstractTableModel() {

	public void setRodape(double valor) {

		NumberFormat formatador = NumberFormat.getInstance();
		formatador.setMaximumFractionDigits(2);
		jLabel2.setText("TOTAL f(v).P(v) " + formatador.format(valor));
	}

	public void setLabelEAG(double valor) {
		NumberFormat formatador = NumberFormat.getInstance();
		formatador.setMaximumFractionDigits(2);
		jLabel3.setText("EAG = sum(f(v).P(v))*8760 = "
				+ formatador.format(valor) + " GWh");

	}

	/** Creates new form ResultadoProdEnergia */
	public ResultadoProdEnergia(AbstractTableModel tabl) {
		super();
		
		tableModel = tabl;
		initComponents();
		this.setSize(500, 500);
		jScrollPane1.setBounds(0, 0, 500, 500);
		jTable1.setBounds(0, 0, 500, 500);
		jPanel1.setBounds(0, 0, 500, 20);
		jLabel1.setBounds(0, 0, 500, 20);
		jTable1.setAutoCreateColumnsFromModel(true);
		jTable1.setAutoCreateRowSorter(true);
		jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);

	}

	public void geraTabela() {
		
		Object[][] array = new Object[4][4];
		
		for (int x = 0; x < 4; x++) {
			for (int y = 0; y < 4; y++) {		
				array[x][y] = x*y;				
			}
		}
		Object[] colunas = { "Col1", "Col1", "Col1", "Col1" };
		
		tableModel = new DefaultTableModel(array,colunas);
//		jTable1.setModel(tableModel);
//		this.repaint();		
//		this.setVisible(true);
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		jScrollPane1 = new javax.swing.JScrollPane();
		jTable1 = new javax.swing.JTable();
		jPanel1 = new javax.swing.JPanel();
		jLabel1 = new javax.swing.JLabel();
		jLabel2 = new javax.swing.JLabel();
		jLabel3 = new javax.swing.JLabel();

		setBackground(new java.awt.Color(255, 255, 255));

		jTable1.setAutoCreateRowSorter(true);
		jTable1.setModel(tableModel);
		jTable1.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
		jTable1.setAutoscrolls(false);
		jScrollPane1.setViewportView(jTable1);

		jPanel1.setBackground(new java.awt.Color(255, 255, 255));

		jLabel1.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
		jLabel1.setText("Tabela. Passos para determinacao da energia anual gerada EAG");

		javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(
				jPanel1);
		jPanel1.setLayout(jPanel1Layout);
		jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				jPanel1Layout
						.createSequentialGroup()
						.addComponent(jLabel1,
								javax.swing.GroupLayout.DEFAULT_SIZE, 390,
								Short.MAX_VALUE).addGap(20, 20, 20)));
		jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				javax.swing.GroupLayout.Alignment.TRAILING,
				jPanel1Layout
						.createSequentialGroup()
						.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE).addComponent(jLabel1)
						.addContainerGap()));

		jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14));
		jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
		jLabel2.setText("jLabel2");

		jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14));
		jLabel3.setText("jLabel3");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addComponent(jLabel2,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										390, Short.MAX_VALUE).addContainerGap())
				.addGroup(
						layout.createSequentialGroup().addContainerGap()
								.addComponent(jLabel3)
								.addContainerGap(357, Short.MAX_VALUE))
				.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
						javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
				.addComponent(jScrollPane1,
						javax.swing.GroupLayout.DEFAULT_SIZE, 410,
						Short.MAX_VALUE));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addComponent(jPanel1,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										25,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(jScrollPane1,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										379, Short.MAX_VALUE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(jLabel2)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
								.addComponent(jLabel3).addGap(19, 19, 19)));
		geraTabela();
	}// </editor-fold>//GEN-END:initComponents

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JTable jTable1;

	// End of variables declaration//GEN-END:variables
	public String getBase64String() {
		String figura = "";
		this.setVisible(true);
		repaint();
		try {
			double escalaX = 1, escalaY = 1;
			Component co = this;
			BufferedImage bi = new BufferedImage(
					(int) (co.getSize().getWidth() * escalaX), (int) (co
							.getSize().getHeight() * escalaY),
					BufferedImage.TYPE_INT_RGB);
			Graphics2D g2d = bi.createGraphics();
			g2d.scale(escalaX, escalaY);
			co.paint(g2d);

			byte[] figBytes;

			figBytes = ChartUtilities.encodeAsPNG(bi);

			BASE64Encoder encoder = new BASE64Encoder();
			figura = encoder.encode(figBytes);
		} catch (Exception ex) {
			Logger.getLogger(Weibull.class.getName()).log(Level.SEVERE, null,
					ex);
		}
		return figura;
	}

	/**
	 * @return the tableModel
	 */
	public AbstractTableModel getTableModel() {
		return tableModel;
	}

	/**
	 * @param tableModel
	 *            the tableModel to set
	 */
	public void setTableModel(AbstractTableModel tableModel) {
		this.tableModel = tableModel;
	}

}
